/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatWindow;

import java.io.IOException;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import login.LoginFrameController;
import main.ConnectionSigleton;

/**
 *
 * @author linux
 */
public class ThreadClient implements Runnable {
    
    @Override
    public void run() {
        while (true){
                
                String msg_recv = new String();
                int error = 0;
                try{
                    //Espera a leer del socket
                    msg_recv = ConnectionSigleton.getInstance().readSocket();
                }catch (IOException ex){
                    if(ex.getMessage().compareTo("Connection Closed") == 0) {
                        System.out.println("IOException: Connection Closed");
                        //continue;
                        break;
                    }
                }
                    int k=0;
                    String command = "";
                    while (!(msg_recv.charAt(k) == '\n')){ 
                        System.out.println("COMMAND "+k+": "+msg_recv.charAt(k));
                        command = command+msg_recv.charAt(k);
                        k++;
                    }
                    
                    System.out.println("COMPLETE COMMAND: "+command);
                    k++;
                    
                    if (command.compareTo("new_msg")==0){
                        int length = 0;
                        String aux = "";
                        String msg = "";
                        String username="";
                        
                        //nickname del que envio el mensaje
                        while (!(msg_recv.charAt(k) == '\n')){
                            username = username+msg_recv.charAt(k);
                            k++;
                        }

                        k++;

                        while (!(msg_recv.charAt(k) == '\n')){

                            aux = aux+msg_recv.charAt(k);
                            k++;
                        }
                        length = Integer.parseInt(aux);
                        k++;
                        
                        int x = k+length;
                        while (k<x){    
                            msg = msg+msg_recv.charAt(k);
                            k++;
                        }
                        ChatWindowFrameController.getInstance().getCwFrame().setAllChat( ChatWindowFrameController.getInstance().getCwFrame().getAllChat()+"\n"+username +" says: "+msg);
                    }
                    if (command.compareTo("user_up")==0){

                        String username = "";
                         while (!((msg_recv.charAt(k) == '\n') && (msg_recv.charAt(k+1) == '\n'))){

                            username = username+msg_recv.charAt(k);
                            k++;
                        }

                        ChatWindowFrameController.getInstance().getCwFrame().addToAllMembers(username);

                    }
                    if (command.compareTo("user_down")==0){
                        String username = "";
                        while(!((msg_recv.charAt(k) == '\n') && (msg_recv.charAt(k+1) == '\n'))){

                            username = username+msg_recv.charAt(k);
                            k++;
                        }

                        ChatWindowFrameController.getInstance().getCwFrame().removeFromAllMembers(username);
                    }
                    if (command.compareTo("login_ok")==0){
                        String allLogged [] = new String[100];
                        
                        allLogged = msg_recv.substring(k, msg_recv.length()).split("\n");
                        
                        LoginFrameController.getInstance().getlFrame().login_error = new Integer(error);
                        
                        while (ChatWindowFrameController.getInstance().getCwFrame() == null){
                            try {
                                Thread.sleep(20);
                            } catch (InterruptedException ex) {
                                Logger.getLogger(ThreadClient.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                        
                        ChatWindowFrameController.getInstance().getCwFrame().addAllMembers(allLogged);
                    }
                    if (command.compareTo("login_error")==0){
                        String aux = "";
                        
                        while (!(msg_recv.charAt(k) == '\n')){

                            aux = aux+msg_recv.charAt(k);
                            k++;
                        }
                        error = Integer.parseInt(aux);
                    }
                    
                    LoginFrameController.getInstance().getlFrame().login_error = new Integer(error);
        } 
    }
    
}
